<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="小区" prop="communityId">
				<el-select v-model="queryWbu.communityId" placeholder="请选择小区" @keyup.enter.native="handleQuery">
					<el-option v-for="item in communityList" :key="item.communityId" :label="item.communityName"
						:value="item.communityId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="楼座" prop="balconyId">
				<el-select v-model="queryWbu.balconyId" placeholder="请选择楼座" @keyup.enter.native="handleQuery">
					<el-option v-for="item in balconyList" :key="item.balconyId" :label="item.balconyName"
						:value="item.balconyId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="单元" prop="unitId">
				<el-select v-model="queryWbu.unitId" placeholder="请选择单元" @keyup.enter.native="handleQuery">
					<el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName" :value="item.unitId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="房号" prop="homeId">
				<el-select v-model="queryWbu.homeId" placeholder="请选择房号" @keyup.enter.native="handleQuery">
					<el-option v-for="item in homeList" :key="item.homeId" :label="item.homeNumber"
						:value="item.homeId">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="合并起始时间" prop="makeStartTime">
				<el-date-picker clearable v-model="queryParams.makeStartTime" type="date" value-format="yyyy-MM-dd"
					placeholder="请选择合并起始时间">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="合并终止时间" prop="makeEndTime">
				<el-date-picker clearable v-model="queryParams.makeEndTime" type="date" value-format="yyyy-MM-dd"
					placeholder="请选择合并终止时间">
				</el-date-picker>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['charge:make:add']">费用生成</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['charge:make:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['charge:make:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['charge:make:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="makeList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="编号" align="center" prop="makeId" />
			<el-table-column label="小区" align="center" prop="wyCommunityVo.communityName" />
			<el-table-column label="楼座" align="center" prop="wyBalconyunithomeVo.balconyName" />
			<el-table-column label="单元" align="center" prop="wyBalconyunithomeVo.unitName" />
			<el-table-column label="房间" align="center" prop="wyBalconyunithomeVo.homeNumber" />
			<el-table-column label="费用内容" align="center" prop="makeContent" />
			<el-table-column label="合并起始时间" align="center" prop="makeStartTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.makeStartTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="合并终止时间" align="center" prop="makeEndTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.makeEndTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="费用生成时间" align="center" prop="makeCreateTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.makeCreateTime, '{y}-{m}-{d}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="合计费用" align="center" prop="makeTotal" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['charge:make:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['charge:make:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改费用生成对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="小区" prop="communityId">
					<el-select v-model="queryWbu.communityId" placeholder="请选择小区" @keyup.enter.native="handleQuery">

						<el-option v-for="item in communityList" :key="item.communityId" :label="item.communityName"
							:value="item.communityId">

						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="楼座" prop="balconyId">
					<el-select v-model="queryWbu.balconyId" placeholder="请选择楼座" @keyup.enter.native="handleQuery">
						<el-option v-for="item in balconyList" :key="item.balconyId" :label="item.balconyName"
							:value="item.balconyId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="单元" prop="unitId">
					<el-select v-model="queryWbu.unitId" placeholder="请选择单元" @keyup.enter.native="handleQuery">
						<el-option v-for="item in unitList" :key="item.unitId" :label="item.unitName"
							:value="item.unitId">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="房号" prop="homeId">
					<el-select v-model="queryWbu.homeId" placeholder="请选择房号" @keyup.enter.native="handleQuery">
						<el-option v-for="item in homeList" :key="item.homeId" :label="item.homeNumber"
							:value="item.homeId">
						</el-option>
					</el-select>
				</el-form-item>

				<!-- <el-form-item label="合并起始时间" prop="makeStartTime">
					<el-date-picker clearable v-model="form.makeStartTime" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择合并起始时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="合并终止时间" prop="makeEndTime">
					<el-date-picker clearable v-model="form.makeEndTime" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择合并终止时间">
					</el-date-picker>
				</el-form-item> -->
				<el-form-item label="选择月份" prop="makeEndTime">
					<el-date-picker v-model="month" value-format="yyyy-MM-dd" type="monthrange" align="right"
						unlink-panels range-separator="至" start-placeholder="开始月份" end-placeholder="结束月份"
						:picker-options="pickerOptions">
					</el-date-picker>
					<el-form-item>{{this.month}}</el-form-item>

				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listMake,
		getMake,
		delMake,
		addMake,
		updateMake
	} from "@/api/charge/make";
	import {
		getCommunity,
		getBalconyUnitHome
	} from "@/api/charge/allocation";

	export default {
		name: "Make",
		data() {
			return {
				pickerOptions: {
					shortcuts: [{
						text: '本月',
						onClick(picker) {
							// 获取当前日期
							const currentDate = new Date();

							// 获取本月的第一天
							const firstDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);

							// 获取本月的最后一天
							const lastDayOfMonth = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1,
								0);

							// 发出本月第一天到本月最后一天的日期范围
							picker.$emit('pick', [firstDayOfMonth, lastDayOfMonth]);
						}
					}, {
						text: '今年至今',
						onClick(picker) {
							const end = new Date();
							const start = new Date(new Date().getFullYear(), 0);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近六个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setMonth(start.getMonth() - 6);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
				month: null,
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 费用生成表格数据
				makeList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					communicateId: null,
					balconyunithomeIds: [],
					makeStartTime: null,
					makeEndTime: null,
				},
				// 表单参数
				form: {
					balconyunithomeIds: []
				},
				// 表单校验
				rules: {

					makeEndTime: [{
						required: true,
						message: '请选择时间范围',
						trigger: 'blur'
					}],
				},
				// 小区列表数据
				communityList: [],
				// 楼座列表数据
				balconyList: [],
				// 单元列表数据
				unitList: [],
				// 房间列表数据
				homeList: [],
				queryWbu: {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				},
			};
		},
		created() {
			this.getList();
			this.getCommunityList()

		},
		methods: {
			/** 查询费用生成列表 */
			getList() {
				this.loading = true;
				listMake(this.queryParams).then(response => {
					this.makeList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
				this.queryParams = {
					pageNum: 1,
					pageSize: 10,
					communicateId: null,
					balconyunithomeIds: [],
					makeStartTime: null,
					makeEndTime: null,
				};
				this.month = null;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					makeId: null,
					communicateId: null,
					balconyunithomeId: null,
					makeContent: null,
					makeStartTime: null,
					makeEndTime: null,
					makeCreateTime: null,
					makeTotal: null,
					del: null
				};


				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.queryWbu = {
					communityId: null,
					balconyId: null,
					unitId: null,
					homeId: null
				};
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.makeId)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加费用生成";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const makeId = row.makeId || this.ids
				getMake(makeId).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改费用生成";
				});
			},
			/** 提交按钮 */
			submitForm() {
				console.log(this.month)
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.makeId != null) {
							updateMake(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.cancel()
								this.getList();
							});
						} else {
							addMake(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.cancel()
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const makeIds = row.makeId || this.ids;
				this.$modal.confirm('是否确认删除费用生成编号为"' + makeIds + '"的数据项？').then(function() {
					return delMake(makeIds);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('charge/make/export', {
					...this.queryParams
				}, `make_${new Date().getTime()}.xlsx`)
			},
			/* 获取小区列表 */
			getCommunityList() {
				getCommunity().then(res => {
					this.communityList = res.data
				})
			},
		},
		watch: {
			'queryWbu.communityId': {
				handler() {

					this.balconyList = []
					this.unitList = []
					this.homeList = []
					this.queryWbu.balconyId = null
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null

					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.balconyList = res.data[0]
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
							this.form.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
						// this.form.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.balconyId': {
				handler() {


					this.unitList = []
					this.homeList = []
					this.queryWbu.unitId = null
					this.queryWbu.homeId = null

					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.unitList = res.data[1]
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
							this.form.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.unitId': {
				handler() {

					this.homeList = []
					this.queryWbu.homeId = null

					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						this.homeList = res.data[2]
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
							this.form.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId
					})
				},
				deep: true,
				immediate: true
			},
			'queryWbu.homeId': {
				handler() {
					this.queryParams.balconyunithomeIds = null
					this.queryParams.communicateId = null
					getBalconyUnitHome(this.queryWbu).then(res => {
						if (res.data.length > 3) {
							this.queryParams.balconyunithomeIds = res.data[3]
							this.form.balconyunithomeIds = res.data[3]
						}
						this.queryParams.communicateId = this.queryWbu.communityId

					})
				},
				deep: true,
				immediate: true
			},
			month(newDate) {
				if (newDate == null) {
					this.form.makeStartTime = null;
					this.form.makeEndTime = null;
				} else {
					this.form.makeStartTime = newDate[0]
					this.form.makeEndTime = newDate[1]
				}

			}
		}
	};
</script>